/**
* 基础菜单 岗位管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>岗位管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline"  ref="formInline" class="user-search" >
      <el-form-item label="搜索：" prop="postName">
        <el-input size="small" v-model="formInline.postName" placeholder="请输入岗位名称"></el-input>
      </el-form-item>


      <el-form-item  label=""  prop="status" >
        <el-select size="small" v-model="formInline.status" placeholder="请选择">
          <el-option label="全部" value=""></el-option>
          <el-option label="启用" value="0"></el-option>
          <el-option label="禁用" value="1"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button> <el-button size="small" type="primary" icon="el-icon-delete" @click="clearForm">清空</el-button>
      </el-form-item>

    </el-form>

    <el-row size="small">
      <el-button size="small" type="primary" icon="el-icon-plus" @click="addPost" v-has="'sys:post:add'">添加</el-button>
    </el-row>
    <br>

    <!-- 岗位列表 -->
    <el-table size="small" @selection-change="selectChange" :data="postData" highlight-current-row v-loading="loading" element-loading-background="rgba(0, 0, 0, 0.1)" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="50">
      </el-table-column>

      <el-table-column align="center" sortable prop="postName" label="岗位名称" width="120">
      </el-table-column>

      <el-table-column align="center" sortable prop="postSort" label="岗位排序" width="120">
      </el-table-column>

      <el-table-column align="center" sortable prop="status" label="岗位状态" width="120">
        <template slot-scope="scope">
         <!-- <div v-if="scope.row.status==1">禁用</div>
          <div v-else="scope.row.status==0">启用</div>-->
          <el-tag type="danger" v-if="scope.row.status==1">禁用</el-tag>
          <el-tag type="success" v-else="scope.row.status==0">启用</el-tag>
        </template>
      </el-table-column>


      <el-table-column align="center" sortable prop="remark" label="备注" width="240">
      </el-table-column>


      <el-table-column align="center" sortable prop="createTime" label="创建时间" min-width="120">
      </el-table-column>

      <el-table-column align="center" sortable prop="updateTime" label="更新时间" min-width="120">
      </el-table-column>

      <el-table-column label="操作" min-width="250">
        <template slot-scope="scope">
          <el-button size="mini" type="success"  icon="el-icon-edit" @click="editPost(scope.$index, scope.row)" v-has="'sys:post:edit'">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="deletePost(scope.$index, scope.row)" v-has="'sys:post:delete'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 分页组件 -->
    <pagination v-bind:child-msg="pageInfo" @callFather="callFather"></pagination>

    <!-- 添加编辑界面 -->
    <el-dialog :title="title" :visible.sync="PostFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="80px" ref="postForm" :model="postForm" :rules="postFormRules">

        <el-form-item label="岗位名称" prop="postName">
          <el-input size="small" v-model="postForm.postName"  placeholder="请输入岗位名称" @keyup.enter.native="search"></el-input>
        </el-form-item>

        <el-form-item label="岗位排序" prop="postSort">
          <el-input-number size="small" v-model="postForm.postSort"  controls-position="right"  :min="1" ></el-input-number>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio v-model="postForm.status" :label="0" >启用</el-radio>
          <el-radio v-model="postForm.status" :label="1">禁用</el-radio>
        </el-form-item>
      <!--  <el-form-item label="状态" prop="status">
          <el-select size="small" v-model="postForm.status" placeholder="请选择" >
            <el-option label="启用" :value="0"></el-option>
            <el-option label="禁用" :value="1"></el-option>
          </el-select>
        </el-form-item>-->

        <el-form-item label="备注" prop="remark">
          <el-input size="small" v-model="postForm.remark" placeholder="请输入备注"></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('postForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import pagination from '@/components/pagination'
  export default {
    data() {
      return {
        // 请求数据参数
        formInline: {
          page: 1,
          pageSize: 10,
          postName: '',
          status:''
        },
        loading: false, //是显示加载
        PostFormVisible: false, //控制用户添加页面显示与隐藏
        title: "添加用户",
        postData:[], // 用户列表数据
        selectData: [], // 选择数据
        postForm:{
          postId:'',
          postName:'',
          postSort:'',
          status:"",
          remark:''
        },
        // 分页参数
        pageInfo: {
          currentPage: 1,
          pageSize: 10,
          total: 0
        },
        // rules表单验证
        postFormRules: {
          postName: [
            { required: true, message: '请输入岗位名称', trigger: 'blur' }
          ],
          postSort: [
            { required: true, message: '请输入岗位排序', trigger: 'blur' },
            {
              pattern: /^[1-9]*[0-9]*$/,
              required: true,
              message: '请输入正确正整数',
              trigger: 'blur'
            }
          ],
          status: [
            { required: true, message: '请选择状态', trigger: 'blur' }
          ]
        },
      }
    },
    // 注册组件
    components: {
      pagination
    },
    created(){
      this.getPostList()
    },
    methods: {
      search(){
        this.getPostList()
      },
      clearForm(){
        if (this.$refs['formInline']) {
          this.$refs['formInline'].resetFields()
        }
        this.getPostList()
      },
      getPostList(){
        this.loading = true;
        this.$axios.getPostList(this.formInline).then(res=>{
          this.loading=false
          if (res.code === 0) {
            this.postData=res.data.list;
            this.pageInfo.total=res.data.totalCount
          }

        }).catch(err=>{
          this.loading=false
          console.log("getPostListErr",err);
        })
      },
      // 选择复选框事件
      selectChange(val) {
        this.selectData = val
        console.log('selectData',this.selectData)
      },
      editPost(index,row){
        this.PostFormVisible = true
        this.title="编辑岗位"
        if (this.$refs['postForm']) {
          this.$refs['postForm'].resetFields()
        }
        this.postForm.postId=row.postId
        this.postForm.postName=row.postName
        this.postForm.postSort=row.postSort
        // 此处有坑如果服务器返回的是字符串的需要转Number不然不会回显
        this.postForm.status=Number(row.status)
        this.postForm.remark=row.remark
      },
      deletePost(index,row){
        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.deletePost({id:row.postId}).then(res=>{
            if (res.code===0) {
              this.getPostList()
              this.$message({
                type: 'success',
                message: '删除成功！'
              })
            }
          }).catch(err=>{
              console.log('deletePostErr',err)
          })
        })
      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage
        this.formInline.pageSize = parm.pageSize
        this.getUserList(this.formInline)
      },
      // 关闭编辑、增加弹出框
      closeDialog(dialog) {
        if (dialog == 'edit') {
          this.PostFormVisible = false
        }
      },
      addPost(){
        this.PostFormVisible = true
        this.title='添加岗位'
        if( this.$refs['postForm']){
          this.$refs['postForm'].resetFields()
        }
        this.postForm.postId=""
        this.postForm.postName=""
        this.postForm.postSort=""
        this.postForm.status=""
        this.postForm.remark=""
      },
      submitForm(postForm){
        this.$refs[postForm].validate(valid => {

          if (valid) {
            this.loading = true
            // 请求方法
            if(this.postForm.postId){
               this.$axios.updatePost(this.postForm).then(res=>{
                 this.loading = false
                 if (res.code===0) {
                   this.PostFormVisible = false
                   this.getPostList()
                   this.$message({
                     type: 'success',
                     message: '修改成功！'
                   })
                 }
               }).catch(err=>{
                 console.log('updatePostErr',err);
                 this.loading = false
               })
            }else{
              this.$axios.savePost(this.postForm).then(res => {
                  this.loading = false
                  if (res.code===0) {
                    this.PostFormVisible = false
                    this.getPostList()
                    this.$message({
                      type: 'success',
                      message: '保存成功！'
                    })
                  }
                }).catch(err=>{
                  console.log('savePostErr',err);
                  this.loading = false
              })


            }
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }
</style>


